package app.ods;

import com.alibaba.fastjson.JSONObject;
import utils.DataGeneratorUtil;

import java.util.ArrayList;
import java.util.Random;

/**
 * 市场分析模拟数据生成器
 */
public class MarketDataGenerator {

    public static void main(String[] args) {
        // 生成商品数据 - 增加到20000条（为了支持多维度分析和榜单功能）
        generateProductData(20000, "ods_market_product");
        
        // 生成搜索关键词数据 - 增加到10000条（为了支持搜索热词、飙升词和蓝海词分析）
        generateSearchKeywordData(10000, "ods_market_search");
        
        // 生成类目数据 - 增加到500条（为了支持类目挖掘和洞察功能）
        generateCategoryData(500, "ods_market_category");
        
        // 生成店铺数据 - 增加到500条（为了支持店铺榜单和多维度分析）
        generateShopData(500, "ods_market_shop");
        
        // 生成内容数据 - 增加到2000条（为了支持内容榜单和效果分析）
        generateContentData(2000, "ods_market_content");
        
        // 生成品牌数据 - 新增，为了更好地支持品牌维度分析
        generateBrandData(100, "ods_market_brand");
    }

    /**
     * 生成商品数据
     */
    public static void generateProductData(int count, String topic) {
        ArrayList<JSONObject> products = new ArrayList<>();
        Random random = new Random();
        long baseTs = System.currentTimeMillis();

        for (int i = 0; i < count; i++) {
            JSONObject product = new JSONObject();
            product.put("product_id", "p" + (10000 + i));
            product.put("product_name", "商品" + (10000 + i));
            product.put("category_id", "c" + (random.nextInt(20) + 1));
            product.put("brand_id", "b" + (random.nextInt(10) + 1));
            product.put("price", 100 + random.nextDouble() * 900);
            product.put("stock", 100 + random.nextInt(900));
            product.put("shop_id", "s" + (random.nextInt(500) + 1)); // 增加到500个店铺，与generateShopData保持一致
            product.put("sales_volume", random.nextInt(10000));
            product.put("sales_amount", product.getDouble("sales_volume") * product.getDouble("price"));
            product.put("visitor_count", product.getInteger("sales_volume") * (5 + random.nextInt(20)));
            product.put("conversion_rate", product.getInteger("sales_volume") / (double) product.getInteger("visitor_count"));
            // 使用更安全的方式生成随机时间戳，避免int溢出
            product.put("create_time", baseTs - random.nextLong() % (30L * 24 * 3600 * 1000));
            product.put("update_time", baseTs - random.nextLong() % (7L * 24 * 3600 * 1000));
            product.put("ts", System.currentTimeMillis());
            
            products.add(product);
        }

        DataGeneratorUtil.sendToKafka(products, topic);
    }

    /**
     * 生成搜索关键词数据
     */
    public static void generateSearchKeywordData(int count, String topic) {
        ArrayList<JSONObject> keywords = new ArrayList<>();
        Random random = new Random();
        
        String[] baseKeywords = {"手机", "笔记本", "衣服", "鞋子", "食品", "化妆品", "电子产品", "家居用品", "图书", "汽车用品"};
        String[] modifiers = {"新款", "热销", "性价比高", "品牌", "特价", "限时", "官方正品", "高清", "智能", "无线"};

        for (int i = 0; i < count; i++) {
            String baseKeyword = baseKeywords[random.nextInt(baseKeywords.length)];
            String keyword;
            
            if (random.nextBoolean()) {
                keyword = modifiers[random.nextInt(modifiers.length)] + baseKeyword;
            } else {
                keyword = baseKeyword;
            }
            
            JSONObject keywordObj = new JSONObject();
            keywordObj.put("keyword", keyword);
            keywordObj.put("search_count", 1000 + random.nextInt(9000));
            keywordObj.put("click_count", keywordObj.getInteger("search_count") * (random.nextInt(50) + 10) / 100);
            keywordObj.put("click_rate", keywordObj.getInteger("click_count") / (double) keywordObj.getInteger("search_count"));
            keywordObj.put("conversion_count", keywordObj.getInteger("click_count") * (random.nextInt(10) + 1) / 100);
            keywordObj.put("conversion_rate", keywordObj.getInteger("conversion_count") / (double) keywordObj.getInteger("click_count"));
            keywordObj.put("demand_supply_ratio", 0.5 + random.nextDouble() * 9.5);
            keywordObj.put("category_id", "c" + (random.nextInt(20) + 1));
            keywordObj.put("trend_type", random.nextBoolean() ? "热搜" : "飙升");
            keywordObj.put("rank", i + 1);
            keywordObj.put("period", "7d");
            keywordObj.put("ts", System.currentTimeMillis());
            
            keywords.add(keywordObj);
        }

        DataGeneratorUtil.sendToKafka(keywords, topic);
    }

    /**
     * 生成类目数据
     */
    public static void generateCategoryData(int count, String topic) {
        ArrayList<JSONObject> categories = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            JSONObject category = new JSONObject();
            String categoryId = "c" + (i + 1);
            category.put("category_id", categoryId);
            category.put("category_name", "类目" + (i + 1));
            category.put("parent_id", i < 20 ? "0" : "c" + (random.nextInt(20) + 1));
            category.put("level", i < 20 ? 1 : 2);
            category.put("product_count", 100 + random.nextInt(900));
            category.put("total_sales_amount", 100000 + random.nextDouble() * 900000);
            category.put("total_sales_volume", (int)(category.getDouble("total_sales_amount") / (200 + random.nextDouble() * 800)));
            category.put("growth_rate", -0.2 + random.nextDouble() * 0.8);
            category.put("demand_supply_ratio", 0.5 + random.nextDouble() * 9.5);
            category.put("period", "7d");
            category.put("ts", System.currentTimeMillis());
            
            categories.add(category);
        }

        DataGeneratorUtil.sendToKafka(categories, topic);
    }

    /**
     * 生成店铺数据
     */
    public static void generateShopData(int count, String topic) {
        ArrayList<JSONObject> shops = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            JSONObject shop = new JSONObject();
            shop.put("shop_id", "s" + (i + 1));
            shop.put("shop_name", "店铺" + (i + 1));
            shop.put("shop_type", random.nextBoolean() ? "淘宝" : "天猫");
            shop.put("avg_price", 100 + random.nextDouble() * 900);
            shop.put("product_count", 50 + random.nextInt(950));
            shop.put("total_sales_amount", 1000000 + random.nextDouble() * 9000000);
            shop.put("total_sales_volume", (int)(shop.getDouble("total_sales_amount") / shop.getDouble("avg_price")));
            shop.put("growth_rate", -0.2 + random.nextDouble() * 0.8);
            shop.put("visitor_count", shop.getInteger("total_sales_volume") * (10 + random.nextInt(50)));
            shop.put("follower_count", 1000 + random.nextInt(99000));
            shop.put("period", "7d");
            shop.put("ts", System.currentTimeMillis());
            
            shops.add(shop);
        }

        DataGeneratorUtil.sendToKafka(shops, topic);
    }

    /**
     * 生成内容数据
     */
    public static void generateContentData(int count, String topic) {
        ArrayList<JSONObject> contents = new ArrayList<>();
        Random random = new Random();
        
        String[] contentTypes = {"直播", "短视频", "图文"};

        for (int i = 0; i < count; i++) {
            JSONObject content = new JSONObject();
            content.put("content_id", "content" + (i + 1));
            content.put("content_title", "内容标题" + (i + 1));
            content.put("content_type", contentTypes[random.nextInt(contentTypes.length)]);
            content.put("shop_id", "s" + (random.nextInt(500) + 1)); // 增加到500个店铺，与generateShopData保持一致
            content.put("view_count", 10000 + random.nextInt(90000));
            content.put("click_count", content.getInteger("view_count") * (random.nextInt(30) + 10) / 100);
            content.put("order_count", content.getInteger("click_count") * (random.nextInt(10) + 1) / 100);
            content.put("sales_amount", content.getInteger("order_count") * (200 + random.nextDouble() * 800));
            content.put("conversion_rate", content.getInteger("order_count") / (double) content.getInteger("view_count"));
            content.put("rank", i + 1);
            content.put("period", "7d");
            content.put("ts", System.currentTimeMillis());
            
            contents.add(content);
        }

        DataGeneratorUtil.sendToKafka(contents, topic);
    }
    
    /**
     * 生成品牌数据
     */
    public static void generateBrandData(int count, String topic) {
        ArrayList<JSONObject> brands = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            JSONObject brand = new JSONObject();
            String brandId = "b" + (i + 1);
            brand.put("brand_id", brandId);
            brand.put("brand_name", "品牌" + (i + 1));
            brand.put("category_id", "c" + (random.nextInt(500) + 1)); // 与类目数据保持一致
            brand.put("product_count", 50 + random.nextInt(950));
            brand.put("total_sales_amount", 1000000 + random.nextDouble() * 9000000);
            brand.put("total_sales_volume", (int)(brand.getDouble("total_sales_amount") / (200 + random.nextDouble() * 800)));
            brand.put("growth_rate", -0.2 + random.nextDouble() * 0.8);
            brand.put("brand_popularity", 1 + random.nextDouble() * 9); // 品牌知名度评分1-10
            brand.put("period", "7d");
            brand.put("ts", System.currentTimeMillis());
            
            brands.add(brand);
        }

        DataGeneratorUtil.sendToKafka(brands, topic);
    }
}